package com.winning.gjhlht.ihe.hl7ClientService.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.winning.common.data.service.PlatBasicDataService;
import com.winning.gjhlht.ihe.hl7ClientService.data.service.IHEDataService;
import com.winning.gjhlht.ihe.hl7ClientService.data.service.IHEDepartDataService;
import com.winning.gjhlht.ihe.hl7ClientService.data.service.IHEGrxxDataService;
import com.winning.gjhlht.ihe.hl7ClientService.data.service.IHEJkdaDataService;
import com.winning.gjhlht.ihe.hl7ClientService.data.service.IHEYhryDataService;
import com.winning.gjhlht.ihe.hl7ClientService.data.service.IHEYljgDataServicce;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/IHEController")
public class IHEController {

    private static Logger LOGGER = Logger.getLogger("pbsLoger");

    @Autowired
    private PlatBasicDataService platBasicDataService = null;

    @Autowired
    private IHEDataService iheDataService = null;

    @Autowired
    private IHEGrxxDataService iheGrxxDataService = null;

    @Autowired
    private IHEJkdaDataService iheJkdaDataService = null;

    @Autowired
    private IHEYljgDataServicce iheYljgDataServicce = null;

    @Autowired
    private IHEDepartDataService iheDepartDataService = null;

    @Autowired
    private IHEYhryDataService iheYhryDataService = null;

    @RequestMapping(value = "/getJBXXZCJGInfo", method = RequestMethod.GET)
    public @ResponseBody
    String getJBXXZCJGInfo(ServletRequest request, int ZCLX, String JGDM, String startDate,
                           String endDate, String ZCJG, String ZJHM) {

        String pageNumberStr = request.getParameter("pageNumber");
        String pageSizeStr = request.getParameter("pageSize");

        //LOGGER.info("pageNumberStr = ["+pageNumberStr+"] , pageSizeStr = ["+pageSizeStr+"]" );

        int pageNumber = new Integer(pageNumberStr);
        int pageSize = new Integer(pageSizeStr);

        JSONObject rs = iheGrxxDataService.queryJBXXZCJG(ZCLX, JGDM, startDate, endDate, ZCJG, pageNumber, pageSize,
                ZJHM);

        return rs.toString();

    }

    @RequestMapping(value = "/getJBXXZCJGCount", method = RequestMethod.GET)
    public @ResponseBody
    String getJBXXZCJGCount() {

        JSONObject rs = iheGrxxDataService.queryJBXXZCJGCount();

        return rs.toString();

    }

    @RequestMapping(value = "/getJKDAZCJGCount", method = RequestMethod.GET)
    public @ResponseBody
    String getJKDAZCJGCount(String JGBM, String ZJHM, String startDate, String endDate) {
        JSONObject rs = null;

        Map<String, Object> param = new HashMap<String, Object>();

        //判断参数是否为空，进行条件筛选
        if (!JGBM.equals("") && JGBM != null) {
            param.put("JGBM", JGBM);
        }
        if (!ZJHM.equals("") && ZJHM != null) {
            param.put("ZJHM", ZJHM);
        }
        if (!startDate.equals("") && startDate != null) {
            param.put("startDate", startDate);
        }
        if (!endDate.equals("") && endDate != null) {
            param.put("endDate", endDate);
        }

        rs = iheJkdaDataService.getJKDAZCJGCount(param);

        return rs.toString();
    }

    @RequestMapping(value = "/getJKDAZCJGInfo", method = RequestMethod.GET)
    public @ResponseBody
    String getJKDAZCJGInfo(String WDLX, String JGBM, String ZJHM, String startDate, String endDate,
                           String SCJG, ServletRequest request) {
        JSONObject rs = null;

        //分页信息
        String pageNumberStr = request.getParameter("pageNumber");
        String pageSizeStr = request.getParameter("pageSize");

        int pageNumber = new Integer(pageNumberStr);
        int pageSize = new Integer(pageSizeStr);

        Map<String, Object> param = new HashMap<String, Object>();

        //判断参数是否为空，进行条件筛选
        if (WDLX != null && !WDLX.equals("")) {
            param.put("WDLX", WDLX);
        }
        if (!JGBM.equals("") && JGBM != null) {
            param.put("JGBM", JGBM);
        }
        if (!ZJHM.equals("") && ZJHM != null) {
            param.put("ZJHM", ZJHM);
        }
        if (!startDate.equals("") && startDate != null) {
            param.put("startDate", startDate);
        }
        if (!endDate.equals("") && endDate != null) {
            param.put("endDate", endDate);
        }
        if (!SCJG.equals("") && SCJG != null) {
            param.put("SCJG", SCJG);
        }

        rs = iheJkdaDataService.getJKDAZCJGInfo(pageSize, pageNumber, param);

        return rs.toString();
    }

    @RequestMapping(value = "/uploadJKDA", method = RequestMethod.GET)
    public @ResponseBody
    String uploadJKDA(String ID) {
        JSONObject rs = new JSONObject();

        rs = iheJkdaDataService.uploadCDAFile(ID);

        return rs.toString();
    }

    @RequestMapping(value = "/getResgisterPersion", method = RequestMethod.GET)
    public @ResponseBody
    String getResgisterPersion(String JGBM, String ZJHM, String XM, ServletRequest request) {
        JSONObject json = new JSONObject();

        Map<String, Object> param = new HashMap<String, Object>();

        //判断参数是否为空,筛选参数
        if (JGBM != null && !JGBM.equals("")) {
            param.put("JGBM", JGBM);
        }

        if (ZJHM != null && !ZJHM.equals("")) {
            param.put("ZJHM", ZJHM);
        }

        if (XM != null && !XM.equals("")) {
            param.put("XM", XM);
        }

        //分页信息
        String pageNumberStr = request.getParameter("pageNumber");
        String pageSizeStr = request.getParameter("pageSize");

        int pageNumber = new Integer(pageNumberStr);
        int pageSize = new Integer(pageSizeStr);

        json = iheJkdaDataService.pageQueryRegisterPersion(param, pageNumber, pageSize);

        return json.toString();
    }

    @RequestMapping(value = "/getPersionCDAInfo", method = RequestMethod.GET)
    public @ResponseBody
    String getPersionCDAInfo(String ID) {
        JSONObject json = new JSONObject();

        json = iheJkdaDataService.getPersionCDAInfo(ID);

        return json.toString();

    }

    @RequestMapping(value = "/createCDAByPersion", method = RequestMethod.GET)
    public @ResponseBody
    String createCDAByPersion(String ID, String WDLX) {
        JSONObject json = new JSONObject();

        json = iheJkdaDataService.createAllCDAByPersion(ID, WDLX);

        return json.toString();
    }

    @RequestMapping(value = "/downLoadCDA", method = RequestMethod.POST)
    public void downLoadCDA(String ID, HttpServletResponse response) {
        iheDataService.downLoadCDAFile(ID, response);
    }

    @RequestMapping(value = "/getJcxxHzscxxInfo", method = RequestMethod.GET)
    public @ResponseBody
    String getJcxxHzscxxInfo(String jgbm, String zjhm, String startDate, String endDate,
                             String sczt, String name, ServletRequest request) {
        JSONObject rs = null;

        //分页信息
        String pageNumberStr = request.getParameter("pageNumber");
        String pageSizeStr = request.getParameter("pageSize");

        int pageNumber = new Integer(pageNumberStr);
        int pageSize = new Integer(pageSizeStr);

        Map<String, Object> param = new HashMap<String, Object>();

        //判断参数是否为空，进行条件筛选
        if (!jgbm.equals("") && jgbm != null) {
            param.put("jgbm", jgbm);
        }
        if (!zjhm.equals("") && zjhm != null) {
            param.put("zjhm", zjhm);
        }
        if (!startDate.equals("") && startDate != null) {
            param.put("startDate", startDate);
        }
        if (!endDate.equals("") && endDate != null) {
            param.put("endDate", endDate);
        }
        if (!sczt.equals("") && sczt != null) {
            param.put("sczt", sczt);
        }

        if (!name.equals("") && name != null) {
            param.put("xm", name);
        }

        rs = iheGrxxDataService.pageQueryHzxxInfo(param, pageNumber, pageSize);

        return rs.toString();
    }

    @RequestMapping(value = "/getHzxxSctjInfo", method = RequestMethod.GET)
    public @ResponseBody
    String getHzxxSctjInfo() {
        JSONObject rs = null;

        rs = iheGrxxDataService.getHzxxSctjInfo();

        return rs.toString();
    }

    @RequestMapping(value = "/hzxxZc", method = RequestMethod.GET)
    public @ResponseBody
    String hzxxZc(String zjhm, String grjbxxbsh) {
        JSONObject rs = null;

        rs = iheGrxxDataService.uploadHzxx(zjhm, grjbxxbsh);

        return rs.toString();
    }

    @RequestMapping(value = "/getCDAInfoByID", method = RequestMethod.GET)
    public @ResponseBody
    String getCDAInfoByID(String lsh) {
        JSONObject rs = null;

        rs = iheJkdaDataService.queryCDAInfoByID(lsh);

        return rs.toString();
    }

    @RequestMapping(value = "/getJcxxYljgInfo", method = RequestMethod.GET)
    public @ResponseBody
    String getJcxxYljgInfo(String yljgdm, String yljgmc, String sczt, ServletRequest request) {
        JSONObject rs = null;

        //分页信息
        String pageNumberStr = request.getParameter("pageNumber");
        String pageSizeStr = request.getParameter("pageSize");

        int pageNumber = new Integer(pageNumberStr);
        int pageSize = new Integer(pageSizeStr);

        Map<String, Object> param = new HashMap<String, Object>();

        if (yljgdm != null && !yljgdm.equals("")) {
            param.put("yljgdm", yljgdm);
        }
        if (yljgmc != null && !yljgmc.equals("")) {
            param.put("yljgmc", yljgmc);
        }
        if (sczt != null && !sczt.equals("")) {
            param.put("sczt", sczt);
        }

        rs = iheYljgDataServicce.queryYljgByPage(param, pageNumber, pageSize);

        return rs.toString();
    }

    @RequestMapping(value = "/getJcxxYljgSctj", method = RequestMethod.GET)
    public @ResponseBody
    String getJcxxYljgSctj() {
        JSONObject rs = null;

        rs = iheYljgDataServicce.queryYljgSctj();

        return rs.toString();
    }

    @RequestMapping(value = "/uploadYLJG", method = RequestMethod.GET)
    public @ResponseBody
    String uploadYLJG(String jgbm) {
        JSONObject rs = null;

        rs = iheYljgDataServicce.uploadYLJGInfo(jgbm);

        return rs.toString();
    }

    @RequestMapping(value = "/getJcxxYlKsInfo", method = RequestMethod.GET)
    public @ResponseBody
    String getJcxxYlKsInfo(String yljgdm, String ksmc, String ksdm, String sczt,
                           ServletRequest request) {
        JSONObject rs = null;

        //分页信息
        String pageNumberStr = request.getParameter("pageNumber");
        String pageSizeStr = request.getParameter("pageSize");

        int pageNumber = new Integer(pageNumberStr);
        int pageSize = new Integer(pageSizeStr);

        Map<String, Object> param = new HashMap<String, Object>();

        if (yljgdm != null && !yljgdm.equals("")) {
            param.put("yljgdm", yljgdm);
        }
        if (ksmc != null && !ksmc.equals("")) {
            param.put("ksmc", ksmc);
        }
        if (ksdm != null && !ksdm.equals("")) {
            param.put("ksdm", ksdm);
        }
        if (sczt != null && !sczt.equals("")) {
            param.put("sczt", sczt);
        }

        rs = iheDepartDataService.queryKsxxByPage(param, pageNumber, pageSize);

        return rs.toString();
    }

    @RequestMapping(value = "/getJcxxKsxxSctj", method = RequestMethod.GET)
    public @ResponseBody
    String getJcxxKsxxSctj() {
        JSONObject rs = null;

        rs = iheDepartDataService.queryKsxxSctj();

        return rs.toString();
    }

    @RequestMapping(value = "/uploadKsxx", method = RequestMethod.GET)
    public @ResponseBody
    String uploadKsxx(String ksdm) {
        JSONObject rs = null;

        rs = iheDepartDataService.uploadKSXXInfo(ksdm);

        return rs.toString();
    }

    @RequestMapping(value = "/getJcxxYhryInfo", method = RequestMethod.GET)
    public @ResponseBody
    String getJcxxYhryInfo(String yljgdm, String zgbm, String xm, String sczt, String zjhm,
                           ServletRequest request) {
        JSONObject rs = null;

        //分页信息
        String pageNumberStr = request.getParameter("pageNumber");
        String pageSizeStr = request.getParameter("pageSize");

        int pageNumber = new Integer(pageNumberStr);
        int pageSize = new Integer(pageSizeStr);

        Map<String, Object> param = new HashMap<String, Object>();

        if (yljgdm != null && !yljgdm.equals("")) {
            param.put("yljgdm", yljgdm);
        }
        if (zgbm != null && !zgbm.equals("")) {
            param.put("zgbm", zgbm);
        }
        if (xm != null && !xm.equals("")) {
            param.put("xm", xm);
        }
        if (zjhm != null && !zjhm.equals("")) {
            param.put("zjhm", zjhm);
        }
        if (sczt != null && !sczt.equals("")) {
            param.put("sczt", sczt);
        }

        rs = iheYhryDataService.queryYhryxxByPage(param, pageNumber, pageSize);

        return rs.toString();
    }

    @RequestMapping(value = "/getJcxxYhrySctj", method = RequestMethod.GET)
    public @ResponseBody
    String getJcxxYhrySctj() {
        JSONObject rs = null;

        rs = iheYhryDataService.queryYhrySctj();

        return rs.toString();
    }

    @RequestMapping(value = "/uploadYhry", method = RequestMethod.GET)
    public @ResponseBody
    String uploadYhry(String yljgdm, String zgbm) {
        JSONObject rs = null;

        rs = iheYhryDataService.uploadYHRYXXInfo(yljgdm, zgbm);

        return rs.toString();
    }

}
